package com.lzp.systemopt.camera

import android.app.Activity
import android.hardware.camera2.CameraCharacteristics
import android.util.SparseIntArray
import android.view.Surface
import androidx.annotation.IntDef
import com.lzp.systemopt.camera.impl.Camera2Impl
import com.lzp.systemopt.camera.view.CameraSurfaceView
import com.utils.PathUtils
import java.io.File

/**
 * @description:
 * @author: lxp
 * @date: 2022/1/22 上午10:48
 */
class CameraManager private constructor() :ICameraManager{
    companion object {

        const val ACTION_CAPTURE_PIC = 0
        const val ACTION_CAPTURE_VIDEO = 1

        @IntDef(ACTION_CAPTURE_PIC, ACTION_CAPTURE_VIDEO)
        private annotation class CameraAction

        var mCameraId = 0

        private var mCameraManager: CameraManager? = null

        fun getInstance(): CameraManager {
            if (mCameraManager != null) {
                return mCameraManager!!
            }
            val camera = CameraManager()
            mCameraManager = camera
            return camera
        }

        fun switchCameraId() {
            mCameraId = if (mCameraId == CameraCharacteristics.LENS_FACING_FRONT) {
                CameraCharacteristics.LENS_FACING_BACK
            } else {
                CameraCharacteristics.LENS_FACING_FRONT
            }
        }

        private val ORIENTATIONS_REAR = SparseIntArray().apply {
            append(Surface.ROTATION_0, 90)
            append(Surface.ROTATION_90, 0)
            append(Surface.ROTATION_180, 270)
            append(Surface.ROTATION_270, 180)
        }
        private val ORIENTATIONS_FRONT = SparseIntArray().apply {
            append(Surface.ROTATION_0, 270)
            append(Surface.ROTATION_90, 0)
            append(Surface.ROTATION_180, 90)
            append(Surface.ROTATION_270, 180)
        }
    }

    private var cameraImpl:ICameraManager? = null

    private fun generateImpl():ICameraManager{
        if (cameraImpl!=null){
            return cameraImpl as ICameraManager
        }
        val impl = Camera2Impl()
        cameraImpl = impl
        return impl
    }

    fun getOrientation(rotation:Int):Int{
        return if(mCameraId == CameraCharacteristics.LENS_FACING_FRONT){
            ORIENTATIONS_REAR.get(rotation)
        }else{
            ORIENTATIONS_FRONT.get(rotation)
        }
    }

    fun generatePicturePath(): String {
        return PathUtils.getCacheDir() + File.separator + "IMG_${System.currentTimeMillis()}.jpg"
    }

    fun generateVideoPath(): String {
        return PathUtils.getCacheDir() + File.separator + "VIDEO_${System.currentTimeMillis()}.mp4"
    }

    override fun initCamera(activity: Activity, cameraSurfaceView: CameraSurfaceView) {
       generateImpl().initCamera(activity, cameraSurfaceView)
    }

    override fun setCaptureListener(listener: CaptureListener?) {
        generateImpl().setCaptureListener(listener)
    }

    override fun requestSwitchCamera() {
        generateImpl().requestSwitchCamera()
    }

    override fun releaseCamera(reason: String) {
        generateImpl().releaseCamera(reason)
    }

    override fun startCapturePicPreView() {
        generateImpl().startCapturePicPreView()
    }

    override fun stopCapturePicPreView() {
        generateImpl().stopCapturePicPreView()
    }

    override fun capturePic() {
        generateImpl().capturePic()
    }

    override fun startCaptureVideoPreview() {
        generateImpl().startCaptureVideoPreview()
    }

    override fun stopCaptureVideoPreview() {
        generateImpl().stopCaptureVideoPreview()
    }

    override fun captureVideo() {
        generateImpl().captureVideo()
    }

    override fun stopCaptureVideo() {
        generateImpl().stopCaptureVideo()
    }
}